package m.jclecai.com.view;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import m.jclecai.com.R;
import m.jclecai.com.bean.ItemBean;
import m.jclecai.com.utils.ScreenUtil;

/**
 * Created by ZD on 2017/4/17.
 */

public class LuckyMenu extends RelativeLayout{

    //初始化数据
    float width=0;
    //左边按钮文本 中间按钮文本 右边按钮文本
    TextView rtxt,ctxt,ltxt;
    String lt = "双色球",ct = "属相",rt = "1注";
    String lvalue = "ssq",cvalue = "sx",rvalue = "1";
    ItemBean bean;

    public LuckyMenu(Context context) {
        super(context);
        init();
    }

    public LuckyMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LuckyMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init(){
        bean = new ItemBean(0, "双色球", "ssq" , 6,1,33,16,true,true);
        setGravity(Gravity.CENTER);
        //图片占用1/2的位置
        width = getResources().getDisplayMetrics().widthPixels / 2;
        ltxt = new TextView(getContext());
        ctxt = new TextView(getContext());
        rtxt = new TextView(getContext());
        canvas();
    }

    void canvas(){
        final ImageView menu = new ImageView(getContext());
        menu.setScaleType(ImageView.ScaleType.FIT_XY);
        menu.setImageResource(R.mipmap.zp_menu);
        addView(menu);
        menu.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                LayoutParams layoutParams = (LayoutParams)menu.getLayoutParams();
                layoutParams.width = (int)width;
                layoutParams.height = (int)(menu.getHeight() * width /menu.getWidth());
                menu.setLayoutParams(layoutParams);
                Log.e("height","height:"+layoutParams.height+" width:"+layoutParams.width);
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
                fillText();
            }
        });
    }

    //比例都是按照图片比例计算然后在按照屏幕缩放
    void fillText(){
        //左边按钮
        int padding = ScreenUtil.dip2px(getContext(),5);
        ltxt.setBackgroundResource(R.drawable.lucky_oval_bg);
        ltxt.setPadding(0,0,0,padding);
        ltxt.setTextSize(TypedValue.COMPLEX_UNIT_DIP ,12);
        addView(ltxt);
        ltxt.setGravity(Gravity.CENTER);
        ltxt.setText(lt);
        ltxt.setTextColor(ContextCompat.getColor(getContext(),R.color.font_white));
        LayoutParams llayoutParams = new LayoutParams(getWidth()/8,getWidth()/8);
        llayoutParams.topMargin = (getHeight() - (getHeight()/2));
        ltxt.setLayoutParams(llayoutParams);
        ltxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.leftButton((TextView) v);
            }
        });

        //中间按钮
        ctxt.setBackgroundResource(R.drawable.lucky_oval_bg);
        ctxt.setPadding(0,0,0,padding);
        ctxt.setTextSize(TypedValue.COMPLEX_UNIT_DIP ,13);
        addView(ctxt);
        ctxt.setGravity(Gravity.CENTER);
        ctxt.setText(ct);
        ctxt.setTextColor(ContextCompat.getColor(getContext(),R.color.font_white));
        LayoutParams clayoutParams = new LayoutParams(getWidth()/8 * 6/5,getWidth()/8* 6/5);
        clayoutParams.leftMargin = (getWidth()/16*3 - ScreenUtil.dip2px(getContext(),5));
        clayoutParams.topMargin = ScreenUtil.dip2px(getContext(),5);
        ctxt.setLayoutParams(clayoutParams);
        ctxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.centerButton((TextView) v);
            }
        });


        //右边按钮
        rtxt.setBackgroundResource(R.drawable.lucky_oval_bg);
        rtxt.setPadding(0,0,0,padding);
        rtxt.setTextSize(TypedValue.COMPLEX_UNIT_DIP ,13);
        addView(rtxt);
        rtxt.setGravity(Gravity.CENTER);
        rtxt.setText(rt);
        rtxt.setTextColor(ContextCompat.getColor(getContext(),R.color.font_white));
        LayoutParams rlayoutParams = new LayoutParams(getWidth()/8,getWidth()/8);
        rlayoutParams.topMargin = (getHeight() - (getHeight()/2));
        rlayoutParams.leftMargin = (int)(width / 4 * 3);
        rtxt.setLayoutParams(rlayoutParams);
        rtxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.rightButton((TextView) v);
            }
        });


    }

    private OnMenuClickListener listener;
    public void setMenuClickListener(OnMenuClickListener listener){
        this.listener = listener;
    }

    public interface OnMenuClickListener{
        void leftButton(TextView view);
        void centerButton(TextView view);
        void rightButton(TextView view);
    }

    public void setCaiZhong(ItemBean itemBean){
        if(itemBean == null) return;
        lt = itemBean.getName();
        lvalue = itemBean.getPinyin();
        bean = itemBean;
    }

    public String getCaiZhongValue(){
        return lvalue;
    }

    public String getShuxXiangValue(){
        return cvalue;
    }

    public String getDuZhuValue(){
        return rvalue;
    }

    public void setShuXiang(ItemBean itemBean){
        if(itemBean == null) return;
        ct = itemBean.getName();
        cvalue = itemBean.getPinyin();
    }
    public void setDuZhu(ItemBean itemBean){
        if(itemBean == null) return;
        rt = itemBean.getName();
        rvalue = itemBean.getPinyin();
    }

    //触发回收机制
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        System.gc();
    }

    public ItemBean getCaiPiaoRule(){
        return bean;
    }
}
